<?php
namespace App\Http\Controllers\ApiApp;

use App\DBs\VideoDB;
use Illuminate\Http\Request;

class VideoController extends BaseController
{
    /**
     * 视频
     */

    protected $db;

    public function __construct()
    {
        parent::__construct();
        $this->db = new VideoDB();
    }

    /**
     * 首页视频列表
     */
    public function getIndex(Request $request)
    {
        //接口权限值
        if ($request->appAccess != $this->db->getAccess()) {
            return $this->toAppJson(-1);
        }
        $uid = 1;
        //获取参数
        $page = $request->post('page',1);
        $size = $request->post('size',$this->limit);
        $idstr = $request->post('idstr','');
        $ids = $idstr ? explode(',',$idstr) : array();
        //梳理数据
        $resDb = $this->db->getIndex($page,$size,$ids);
        if ($resDb['code']!=0) {
            return $this->toAppJson(-5,array(),$resDb['msg']);
        }
        //历史扩增
        $resHistory = $this->db->setHistory($uid,$vids);
        if ($resHistory['code']!=0) {
            return $this->toAppJson(-7,array(),$resDb['msg']);
        }
        return $this->toAppJson(0,$resDb['data']);
    }

    /**
     * 单个视频
     */
    public function getOne(Request $request)
    {
        //接口权限值
        if ($request->appAccess != $this->db->getAccess()) {
            return $this->toAppJson(-1);
        }
        $uid = 1;
        //获取参数
        $idstr = $request->post('idstr','');
        $ids = $idstr ? explode(',',$idstr) : array();
        //梳理数据
        $resDb = $this->db->getOne($uid,$ids);
        return $resDb;
        if ($resDb['code']!=0) {
            return $this->toAppJson(-5,array(),$resDb['msg']);
        }
        //历史扩增
        $vids = array($resDb['data']['videoId']);
        $resHistory = $this->db->setHistory($uid,$vids);
        if ($resHistory['code']!=0) {
            return $this->toAppJson(-7,array(),$resDb['msg']);
        }
        return $this->toAppJson(0,$resDb['data']);
    }

//    /**
//     * 视频历史扩增
//     */
//    public function setHistory(Request $request)
//    {
//        //接口权限值
//        if ($request->appAccess != $this->db->getAccess()) {
//            return $this->toAppJson(-1);
//        }
//        $uid = $request->post('vid',1);
//        $vid = $request->post('vid',0);
//        if (!$uid || !$vid) {
//            return $this->toAppJson(-2);
//        }
//        $vids = [$vid];
//        $resDb = $this->db->setHistory($uid,$vids);
//        if ($resDb['code']!=0) {
//            return $this->toAppJson(-6,array(),$resDb['msg']);
//        }
//        return $this->toAppJson(0);
//    }

    /**
     * 视频历史删除
     */
    public function setHistoryDel(Request $request)
    {
        //接口权限值
        if ($request->appAccess != $this->db->getAccess()) {
            return $this->toAppJson(-1);
        }
//        $uid = $request->post('uid',1);
        $uid = 1;
        $vids = $request->post('idstr','');
        if (!$uid || !$vids) {
            return $this->toAppJson(-2);
        }
        $vids = explode(',',$vids);
        $resDb = $this->db->setHistoryDel($uid,$vids);
        if ($resDb['code']!=0) {
            return $this->toAppJson(-10,array(),$resDb['msg']);
        }
        return $this->toAppJson(0);
    }

    /**
     * 点赞增加
     */
    public function setZanAdd(Request $request)
    {
        //接口权限值
        if ($request->appAccess != $this->db->getAccess()) {
            return $this->toAppJson(-1);
        }
        $uid = 1;
        $vid = $request->post('vid',0);
        if (!$uid || !$vid) {
            return $this->toAppJson(-2);
        }
        $resDb = $this->db->setZanAdd($uid,$vid);
        if ($resDb['code']!=0) {
            return $this->toAppJson(-6,array(),$resDb['msg']);
        }
        return $this->toAppJson(0);
    }

    /**
     * 点赞移除
     */
    public function setZanDel(Request $request)
    {
        //接口权限值
        if ($request->appAccess != $this->db->getAccess()) {
            return $this->toAppJson(-1);
        }
        $uid = 1;
        $vid = $request->post('vid',0);
        if (!$uid || !$vid) {
            return $this->toAppJson(-2);
        }
        $resDb = $this->db->setZanDel($uid,$vid);
        if ($resDb['code']!=0) {
            return $this->toAppJson(-10,array(),$resDb['msg']);
        }
        return $this->toAppJson(0);
    }
}
